package com.mindao.app.ued.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mindao.app.common.spring.controller.BaseBaseController;
import com.mindao.app.common.spring.service.util.JsonResult;
import com.mindao.app.ued.service.EventPrizeService;
import com.mindao.app.ued.service.ZooKeeperService;
import com.mindao.app.ued.utils.ZooKeeperKeyConstants;

/**
/* 版权所有： 广州敏道科技有限公司
/*
/* 功能描述：Zookeeper 修改参数演示
/*
/* 创 建 人：李国才
/* 创建时间：2016年8月9日 下午1:01:11 	
 **/
@Controller
@RequestMapping("/ued/zookeeper")
public class ZooKeeperController  extends BaseBaseController{
	@Autowired
	private ZooKeeperService zooKeeperService;
	@Autowired
	private EventPrizeService eventPrizeService;	
	/**
	 * 进入配置页面，读取本地配置
	 * @param request
	 * @param response
	 * @param session
	 * @param rhs
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public String conf(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
		String prizeNumber=zooKeeperService.getParaFromZookeeper(ZooKeeperKeyConstants.prizeNumber);
		rhs.addAttribute("prizeNumber", prizeNumber);
		return  super.rtnDefault(request, response, session, rhs);
	}
	/**
	 * 提交配置，通过Zookeeper更新所有子节点
	 * @param request
	 * @param response
	 * @param session
	 * @param rhs
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping
	public JsonResult submitConfData(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
		JsonResult jr=null;
		String prizeNumber=(String)request.getParameter("prizeNumber");
		try{
			zooKeeperService.setParaFromZookeeper(ZooKeeperKeyConstants.prizeNumber, prizeNumber);
			jr=new JsonResult(JsonResult.SUCCESS,"配置提交成功");
		}catch (Exception e){
			jr=new JsonResult(JsonResult.ERROR,"配置提交失败:"+e.getMessage());
		}
		return  jr;
	}
}
